import { exec } from 'child_process';
import fs from 'fs';
import lodash from 'lodash';
import { createRequire } from 'module';
import path, { dirname } from 'path';
import prettier from 'prettier';
import { fileURLToPath } from 'url';

function copy(params) {
  exec('pbcopy').stdin.end(params);
}

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const require = createRequire(import.meta.url);
const cwd = process.cwd();
const prettierrc = JSON.parse(
  fs.readFileSync(path.resolve(cwd, '.prettierrc'), 'utf-8'),
);
let projectDir = path.resolve(cwd, 'components/teld/basic');
let toDir = path.resolve(cwd, 'components/teld/basic');
let components = fs.readdirSync(projectDir, { encoding: 'utf-8' });

for (let i = 0; i < components.length; i++) {
  let e = components[i];
  let ePath = path.resolve(projectDir, e);
  let kebabCaseName = lodash.kebabCase(e);
  let capitalizeName = lodash.capitalize(e);
  let camelCaseName = lodash.camelCase(e);
  let snakeCaseName = lodash.snakeCase(e);
  let startCaseName = lodash.startCase(e);
  let ComponentsCaseName = lodash.upperFirst(camelCaseName);
  let toPathFile = path.resolve(toDir, kebabCaseName);
  let toPath = toPathFile;

  // if (fs.statSync(ePath).isFile()) {
  //   continue;
  // }
  if (fs.existsSync(path.resolve(ePath, 'index.js'))) {
  } else {
    console.log(e);
  }

  let a = fs.readdirSync(ePath, { encoding: 'utf-8' });
  for (let i_ = 0; i_ < a.length; i_++) {
    const e_ = a[i_];

    if (!fs.statSync(path.resolve(ePath, e_)).isFile()) {
      continue;
    }
    let t = fs.readFileSync(path.resolve(ePath, e_), {
      encoding: 'utf-8',
    });
    if (t.includes('@teld/q-components/components/')) {
      t = t.replace(
        /'t\/quasar\/components\/(.+)'/g,
        e_ === 'Component.jsx'
          ? `'@teld/q-components/${ComponentsCaseName}'`
          : `'@teld/q-components/${e_.replace('.jsx', '')}'`,
      );
      console.log(e_);
      t = await prettier.format(t, { parser: 'typescript' });
      fs.writeFileSync(path.resolve(ePath, e_), t, {
        encoding: 'utf-8',
      });
    }
  }
}
